// 简易封装jQuery的ajax
function get(url, success, error) {
    $(function () {
        $.ajax({
            url: url,
            dataType: "json",
            success: success,
            error: error
        })
    })
}

function post(url, data, success, error) {
    $(function () {
        $.ajax({
            url: url,
            data: data,
            type: "post",
            success: success,
            error: error
        })
    })
}

// 文件上传
function upload(url, data, success, error) {
    $.ajax({
        url: url,
        type: "post",
        data: data,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        success: success,
        error: error
    });
}

// 对swal的简单封装

function swalSuccess(title, description) {
    swalTimer(title, description)
}

function swalError(title, description) {
    swalTimer(title, description, "error")
}

function swalTimer(title, description, level, timer) {
    
    if (level == "" || level == null) {
        level = "success"
    }
    if (timer == "" || timer == null) {
        timer = 1500
    }
    if (description == "" || description == null) {
        swal(title, {
            buttons: false,
            timer: timer,
            icon: level
        })
    } else {
        swal(title, description, {
            buttons: false,
            timer: timer,
            icon: level
        })
    }
}
// 确认
function swalConfirm(title, description, level, success, error) {
    swal({
        title: title,
        text: description,
        icon: level,
        buttons: {
            confirm: "确认",
            cancel: "取消"
        },
        dangerMode: true,
      })
      .then((willDelete) => {
        if (willDelete) {
            swalTimer("操作成功")
            if (success) {
                success()
            }
        } else {
            swalTimer("操作已取消", null, "error")
            if (error) {
                error()
            }
        }
      });
}

// 页面跳转

function jump(href, time, title, description) {
    if ("" == title) {
        title = "跳转页面"
    }
    if ("" == description) {
        description = "请稍后，页面跳转中..."
    }
    swalSuccess(title, description)
    setTimeout(function () {
        location.href = href
    }, time)
}

// 样式切换

function myExchangeClass(selector) {
    $(function(){
        let items = $(selector)
        items.each(function(){
            $(this).click(function(){
                items.each(function(){
                    $(this).removeClass("active")
                })
                $(this).addClass("active")
            })
        })
    })
}


function switchClass(selector, target, urlMenu) {
    $(function(){
        let items = $(selector)
        items.each(function(index){
            $(this).click(function(){
                items.each(function(){
                    $(this).removeClass("active")
                })
                $(this).addClass("active")
                get(urlMenu[index], null, function(data){
                    replaceHtml(target, data.responseText)
                })
            })
        })
    })
}

// 两对象显示与隐藏的绑定
function exchangeDisplay(selectorA, selectorB){
    $(function(){
        let itemsA = $(selectorA), itemsB = $(selectorB)
        itemsA.each(function(index){
            $(this).click(function(){
                itemsA.each(function(){
                    $(this).removeClass("active")
                })
                itemsB.each(function(){
                    $(this).hide()
                })
                $(this).addClass("active")
                itemsB.eq(index).show()
            })
        })
    })
}

// html替换
function replaceHtml(selector, content){
    $(function(){
        $(selector).html(content)
    })
}

// 解析url
function analysisUrl(url, index) {
    let split = url.split("/")
    if (index > split.length - 1) {
        return null;
    }
    return split[index]
}
function addActiveFirst(selector, className) {
    if (className == null || className == "") {
        className = "active"
    }
    $(function(){
        let target = $(selector)
        target.eq(0).addClass(className)
    })
}

function addActive(selector, index) {
    $(function(){
        let target = $(selector)
        target.each(function(){
            $(this).removeClass("active")
        })
        target.eq(index).addClass("active")
    })
}

    
// 自动聚焦
function autoFocus(selector) {
    setTimeout(function(){
        $(selector).focus()
    }, 100)
}
